﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Xww.LCV {
    class Program {
        static void Main(string[] args) {
            try {
                if (args.Length == 0) { ShowHelp(); return; }
                if (args.Length < 3) { ShowArgumentsError(null); return; }
                
                string qSetFile = args[0];
                if (IsRelativePath(qSetFile)) {
                    qSetFile = Path.Combine(Environment.CurrentDirectory, qSetFile);
                }
                if (!File.Exists(qSetFile)) { ShowArgumentsError("Error : QuintessentialSet file doesn't exist."); return; }
                
                int threshold = 0;
                if (!int.TryParse(args[1], out threshold)) {
                    ShowArgumentsError("Error : Threshold MUST be an integer."); return;
                }

                string outputFile = args[2];
                if (IsRelativePath(outputFile)) {
                    outputFile = Path.Combine(Environment.CurrentDirectory, outputFile);
                }

                bool refine = false;
                if (args.Length > 3) {
                    if (string.Equals("-r", args[3], StringComparison.OrdinalIgnoreCase)) {
                        refine = true;
                    }
                    else {
                        ShowArgumentsError("Error : option parameter should be '-r'"); return;
                    }
                }

                Run(qSetFile, threshold, outputFile, refine);
            }
            catch (Exception e) {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                ShowContactInfo();
            }
            finally {
                //Console.WriteLine("Press any key to exit."); //for debug
                //Console.ReadKey();
            }
            
        }

        private static void Run(string qSetFile, int threshold, string outputFile, bool refine) {
            throw new NotImplementedException();
        }

        private static bool IsRelativePath(string path) {
            return !path.Contains("/") && !path.Contains("\\");
        }
        private static void ShowContactInfo() {
            Console.WriteLine("For any questions and comments, please email XiongWenwei@gmail.com");
        }
        private static void ShowArgumentsError(string error) {
            Console.ForegroundColor = ConsoleColor.Red;
            if (string.IsNullOrEmpty(error)) {
                Console.WriteLine("Arguments error");
            }
            else {
                Console.WriteLine(error);
            }
            Console.ResetColor();
            ShowHelp();
            Console.WriteLine();
            ShowContactInfo();
        }
            
        static void ShowHelp() {
            Console.WriteLine(@"Copyright (c) 2007-2009 Wenwei Xiong, XiongWenwei@gmail.com
All rights reserved.

'LCV-Extract' is available at:
ftp://cheminfo.tongji.edu.cn/LCV/

Usage: LCV-Extract QuintessentialSet_File threshold Output_File [options]

params:
QuintessentialSet_File: File name of QuintessentialSet.
threshold: An integer used to filter LCS, which should be greater than 0
and less than the count of sequence in QuintessentialSet.
Output_File: Save LCS collection to local file.

[options]
 -r/-R : Refine LCS collection before saving. 
");
        }
        /*
        private static void Run() {
            LCSCollection rst = Extractor.GetLCSCollection(@"A:\Data\HTH重要数据\MasterSet.txt", 8).Refine();
            Console.WriteLine(rst.GenerateInfo);
            foreach (LCS item in rst) {
                if (item.Length > 3) {
                    Console.WriteLine(item);
                }
            }
        }
        private static void Run(string qSetFile, int threshold, string outputFile, bool refine) {
            LCSCollection rst = Extractor.GetLCSCollection(qSetFile, threshold);
            if (refine) { rst = rst.Refine(); }
            using (StreamWriter writer = new StreamWriter(outputFile)) {
                writer.WriteLine(rst.GenerateInfo);
                writer.WriteLine("LCS\tSupport");
                foreach (LCS item in rst) {
                    writer.WriteLine("{0}\t{1}", item, item.Support);
                }
            }
        }
         * */
    }
}
